package com.metaera.app.ui.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.ui.activities.ActivityDetailsActivity;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.SpannableStringHelper;

import androidx.annotation.NonNull;

public class SearchResultActivityAdapter extends BaseQuickAdapter<SearchResultResponse, BaseViewHolder> {
    private String keyword;

    public SearchResultActivityAdapter(String keyword) {
        super(R.layout.item_search_activity_type);
        this.keyword = keyword;

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(ActivityDetailsActivity.newInstance(context, getItem(position).getId()));
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, SearchResultResponse item) {
        Resources resources = getContext().getResources();

        holder.setText(R.id.tv_title, SpannableStringHelper.matchText(Color.parseColor("#3756FD"), item.getTitle(), keyword))
                .setText(R.id.tv_date, DateUtils.getTime(item.getStart_time_stamp(), DateUtils.PATTERN_10)
                        + " ~ "
                        + DateUtils.getTime(item.getEnd_time_stamp(), DateUtils.PATTERN_10))
                .setText(R.id.tv_location, item.getCity() + " " + item.getArea() + " " + item.getAddress())
                .setText(R.id.tv_group_number, String.format(resources.getString(R.string.publisher_placeholder), item.getOrganizer()));
    }

}
